import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import database.model.Article;
import database.model.Section;


public class SyncManager {
	public static final String sportRssUrl = "http://vnexpress.net/rss/gl/the-thao.rss";
	private static final String exempleArticleHtmlUrl = "http://vnexpress.net/gl/the-thao/bong-da/2011/12/bellamy-toa-sang-liverpool-thang-nguoc-newcastle/";
	private static final String VNEXPRESS_PDA_SUFFIX = "?cboGuidPDA=1";
	public void synchronize()
	{
		System.out.println("begining sync .....");
		//download(sportRssUrl);
		//downloadArticle(exempleArticleHtmlUrl);
		/*Session session = HibernateUtil.currentSession();
		Transaction trans = session.beginTransaction();
		List<Article> list = session.createSQLQuery("select * from ARTICLE").addEntity(Article.class).list();
		for(Article a : list)
		{
			session.delete(a);
		}
		//Article article = (Article) session.createSQLQuery("select * from ARTICLE WHERE url = '" + url + "'").addEntity(Article.class).uniqueResult();
		trans.commit();
		HibernateUtil.closeSession();*/
		//DataBaseStore.defaultStore().clean();
		syncSection(sportRssUrl);
		//Section section = Section.getSectionWithUrl(sportRssUrl);
		/*Session session = HibernateUtil.currentSession();
		Transaction trans = session.beginTransaction();
		List<Section> list =  session.createCriteria(Section.class).setFetchSize(1).add(Restrictions.like("url", sportRssUrl)).list();
		Section section = list.get(0);
		List<Article> articles = section.getArticles();
		for(Article anArticle:articles)
		{
			System.out.println(anArticle.getUrl());
		}
		trans.commit();
		HibernateUtil.closeSession();*/
		
	}
	
	private void syncSection(String urlString)
	{
		URL url;
		HttpURLConnection conection = null;
		
		try {
			url = new URL(urlString);
			
			conection = (HttpURLConnection) url.openConnection();
			if(conection.getResponseCode() == 200)
			{
				//Section section = new Section(conection.getInputStream());
				Section section = Section.update(urlString, conection.getInputStream());
				for(database.model.Article article : section.getArticles())
				{
					downloadArticle(article.getUrl());
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			if(conection != null)
				conection.disconnect();
			
		}
	}
	Article _article;
	
	public Article getArticle(){ return _article; }
	
	private void downloadArticle(String htmlUrl)
	{
		String pdaHtmlUrl = htmlUrl+VNEXPRESS_PDA_SUFFIX;
		URL url;
		try {
			url = new URL(pdaHtmlUrl);
			HttpURLConnection conection = (HttpURLConnection) url.openConnection();
			System.out.println(conection.getContentEncoding());
			if(conection.getResponseCode() == 200)
			{
				//_article = new Article(conection.getInputStream());
				database.model.Article.update(htmlUrl, conection.getInputStream());
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
